package com.sec.etech.bpm.cust.controller;

import com.github.pagehelper.Page;
import com.sec.etech.bpm.cust.dto.EtechBpmTask;
import com.sec.etech.bpm.cust.manager.EtechTaskManagerImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.openbpm.base.api.query.QueryFilter;
import org.openbpm.base.db.model.page.PageResult;
import org.openbpm.base.rest.ControllerTools;
import org.openbpm.bpm.core.manager.BpmTaskManager;
import org.openbpm.bpm.core.model.BpmTask;
import org.openbpm.bpm.rest.controller.BpmMyTaskController;
import org.openbpm.sys.util.ContextUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping({"/bpm/my2"})
@Api(description = "个人办公服务接口")
public class EtechMyTaskController extends ControllerTools {

    @Resource(name="etechTaskManager")
    EtechTaskManagerImpl bpmTaskManager;

    @RequestMapping(value = {"todoTaskList"}, method = {RequestMethod.POST})
    @ApiOperation(value = "我的待办", notes = "根据当前用户获取个人所有待办，支持对subject_,name_等Task字段过滤，“filter”为数据库过滤字段名,“V”一位代表字段类型,“EQ/IN/LK/..”代表查询类型“等于/in/小于/..”")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "String", name = "offset", value = "offset"), @ApiImplicitParam(paramType = "form", dataType = "String", name = "limit", value = "分页大小，默认20条"), @ApiImplicitParam(paramType = "form", dataType = "String", name = "sort", value = "排序字段"), @ApiImplicitParam(paramType = "form", dataType = "String", name = "order", value = "排序，默认升序", defaultValue = "ASC"), @ApiImplicitParam(paramType = "form", dataType = "String", name = "filter$VEQ", value = "过滤参数")})
    public PageResult<EtechBpmTask> todoTaskList2(HttpServletRequest request, HttpServletResponse response) throws Exception {
        QueryFilter queryFilter = getQueryFilter(request);
        String userId = ContextUtil.getCurrentUserId();
        Page<EtechBpmTask> bpmTaskList = (Page)this.bpmTaskManager.getTodoList2(userId, queryFilter);
        return new PageResult(bpmTaskList);
    }


    @RequestMapping(value = {"todoTaskListOrg"}, method = {RequestMethod.POST})
    @ApiOperation(value = "组织的待办", notes = "根据当前用户可查看的组织待办，支持对subject_,name_等Task字段过滤，“filter”为数据库过滤字段名,“V”一位代表字段类型,“EQ/IN/LK/..”代表查询类型“等于/in/小于/..”")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "String", name = "offset", value = "offset"), @ApiImplicitParam(paramType = "form", dataType = "String", name = "limit", value = "分页大小，默认20条"), @ApiImplicitParam(paramType = "form", dataType = "String", name = "sort", value = "排序字段"), @ApiImplicitParam(paramType = "form", dataType = "String", name = "order", value = "排序，默认升序", defaultValue = "ASC"), @ApiImplicitParam(paramType = "form", dataType = "String", name = "filter$VEQ", value = "过滤参数")})
    public PageResult<EtechBpmTask> todoTaskListMasterOrg(HttpServletRequest request, HttpServletResponse response) throws Exception {
        QueryFilter queryFilter = getQueryFilter(request);
        //  同班组的待处理   "WHERE o.master_group_ IN ('${currentGroupIds}')"
        queryFilter.getParams().put("type","todo4managerGroup");
        List list = this.bpmTaskManager.getTodoList2(queryFilter);
        if(list instanceof  Page){
            Page<EtechBpmTask> bpmTaskList = (Page)list;
            return new PageResult(bpmTaskList);
        }
        return new PageResult(list);
    }
}
